package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 添加课程
func AddCourse(c *gin.Context) {
	var req request.AddCourseReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数有误",
			"data": err.Error(),
		})
		return
	}

	addCourse, err := config.CourseClient.AddCourse(c, &__.AddCourseReq{
		CourseTypeId: req.CourseTypeId,
		Author:       req.Author,
		Title:        req.Title,
		Desc:         req.Desc,
		CoverUrl:     req.CoverUrl,
		Catalogue:    req.Catalogue,
		MemberPrice:  float32(req.MemberPrice),
		Price:        float32(req.Price),
		PayUrl:       req.PayUrl,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 500,
			"msg":  "服务端错误",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加课程成功",
		"data": addCourse.CourseId,
	})
}

// 课程列表
func GetCourseList(c *gin.Context) {
	var req request.GetCourseListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数有误",
			"data": err.Error(),
		})
		return
	}

	courseList, err := config.CourseClient.GetCourseList(c, &__.GetCourseListReq{
		CourseTypeId: req.CourseTypeId,
		Page:         req.Page,
		PageSize:     req.PageSize,
		UserId:       req.UserId,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 500,
			"msg":  "服务端错误",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "课程列表展示成功",
		"data": courseList,
	})
}

// 课程详情
func GetCourseDetail(c *gin.Context) {
	var req request.GetCourseDetailReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数有误",
			"data": err.Error(),
		})
		return
	}

	courseDetail, err := config.CourseClient.GetCourseDetail(c, &__.GetCourseDetailReq{
		CourseId: req.CourseId,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 500,
			"msg":  "服务端错误",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "课程详情展示成功",
		"data": courseDetail,
	})
}
